package com.coin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.*;
import com.coin.mapper.SysRolePrivilegeMapper;
import com.coin.model.RolePrivilegesParam;
import com.coin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 李力
 * @description 针对表【sys_role_privilege(角色权限配置)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege>
        implements SysRolePrivilegeService {

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysPrivilegeService privilegeService;

    @Autowired
    private SysRolePrivilegeService rolePrivilegeService;

    /**
     * @Description: 根据角色ID查询菜单
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: roleId
     * @return: R
     */
    @Override
    public List<SysMenu> getMenusByRoleId(Long roleId) {
        if (roleId == null) {
            throw new RuntimeException("参数为空");
        }
        //先查询所有的菜单
        List<SysMenu> list = menuService.list();
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        //页面显示的是二级菜单，以及二级菜单包含的权限
        //一级菜单
        List<SysMenu> rootMenu = list.stream().filter
                (menu -> menu.getParentId() == null).collect(Collectors.toList());
        if (rootMenu.isEmpty()) {
            return Collections.emptyList();
        }
        //查询二级菜单
        List<SysMenu> subMenu = new ArrayList<>();
        for (SysMenu menu : rootMenu) {
            subMenu.addAll(getChildMenus(menu.getId(), roleId, list));
        }
        return subMenu;
    }

    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        List<SysMenu> childs = new ArrayList<>();
        for (SysMenu source : sources) {
            if (Objects.equals(source.getParentId(), parentId)) {
                childs.add(source); //找儿子
                source.setChilds(getChildMenus(source.getId(), roleId, sources)); //给儿子设置儿子
                List<SysPrivilege> privileges = privilegeService.getAllPermissions(source.getId(), roleId);
                source.setPrivileges(privileges);
            }
        }
        return childs;
    }

    /**
     * @Description: 授予角色某种权限
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: rolePrivilegesParam
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        if (rolePrivilegesParam == null) {
            throw new RuntimeException("参数为空");
        }
        if (rolePrivilegesParam.getRoleId() == null || rolePrivilegesParam.getPrivilegeIds().isEmpty()) {
            throw new RuntimeException("参数为空");
        }
        //1.先删除之前的角色对应的权限
        LambdaQueryWrapper<SysRolePrivilege> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(SysRolePrivilege::getRoleId, rolePrivilegesParam.getRoleId());
        boolean remove = rolePrivilegeService.remove(wrapper);
        if (remove) {
            //2.重新新增
            List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
            List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
            for (Long privilegeId : privilegeIds) {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setRoleId(rolePrivilegesParam.getRoleId());
                sysRolePrivilege.setPrivilegeId(privilegeId);
                sysRolePrivileges.add(sysRolePrivilege);
            }
            return rolePrivilegeService.saveBatch(sysRolePrivileges);
        }
        return false;
    }
}




